var contractCode = null;
var flowCode = null;
var enterpriseName = null;
var agent = null;
var phone = null;
var businessCode = null;
var inDate = null;
var price = null;
var contractTerm = null;
var endDate = null;
var payment = null;
var businessType = null;
var bandwidth = null;
var state = null;
var addBtn = null;
var backBtn = null;

function priceReg (value){
	value = value.replace(/[^\d.]/g,"");  //清除“数字”和“.”以外的字符
		value = value.replace(/^\./g,"");  //验证第一个字符是数字而不是.
    value = value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的.
    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");//去掉特殊符号￥
	if(value.indexOf(".")>0){
		value = value.substring(0,value.indexOf(".")+3);
	}
	return value;
}


$(function(){
    contractCode = $("#contractCode");
    flowCode = $("#flowCode");
    enterpriseName = $("#enterpriseName");
    agent = $("#agent");
    phone = $("#phone");
    businessCode = $("#businessCode");
    inDate = $("#inDate");
    price = $("#price");
    contractTerm = $("#contractTerm");
    endDate = $("#endDate");
    payment = $("#payment");
    businessType = $("#businessType");
    bandwidth = $("#bandwidth");
    state = $("#state");

	addBtn = $("#add");
	backBtn = $("#back");
	//初始化的时候，要把所有的提示信息变为：* 以提示必填项，更灵活，不要写在页面上
    contractCode.next().html("*");
    flowCode.next().html("*");
    enterpriseName.next().html("*");
    agent.next().html("*");
    phone.next().html("*");
    businessCode.next().html("*");
    inDate.next().html("*");
    price.next().html("*");
    contractTerm.next().html("*");
    endDate.next().html("*");
    payment.next().html("*");
    businessType.next().html("*");
    bandwidth.next().html("*");
    state.next().html("*");

	/*
	 * 验证
	 * 失焦\获焦
	 * jquery的方法传递
	 */
    contractCode.on("focus", function () {
        validateTip(contractCode.next(), {"color": "#666666"}, "* 请输入合同号", false);
    }).on("blur", function () {
        if (contractCode.val() != null && contractCode.val() != "") {
            validateTip(contractCode.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(contractCode.next(), {"color": "red"}, imgNo + " 合同号不能为空，请重新输入", false);
        }
    });
    flowCode.on("focus", function () {
        validateTip(flowCode.next(), {"color": "#666666"}, "* 请输入流水号", false);
    }).on("blur", function () {
        if (flowCode.val() != null && flowCode.val() != "") {
            validateTip(flowCode.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(flowCode.next(), {"color": "red"}, imgNo + " 流水号不能为空，请重新输入", false);
        }
    });
    enterpriseName.on("focus", function () {
        validateTip(enterpriseName.next(), {"color": "#666666"}, "* 请输入企业名称", false);
    }).on("blur", function () {
        if (enterpriseName.val() != null && enterpriseName.val() != "") {
            validateTip(enterpriseName.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(enterpriseName.next(), {"color": "red"}, imgNo + " 企业名称不能为空，请重新输入", false);
        }
    });
    agent.on("focus", function () {
        validateTip(agent.next(), {"color": "#666666"}, "* 请输入经办人", false);
    }).on("blur", function () {
        if (agent.val() != null && agent.val() != "") {
            validateTip(agent.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(agent.next(), {"color": "red"}, imgNo + " 经办人不能为空，请重新输入", false);
        }
    });
    phone.on("focus", function () {
        validateTip(phone.next(), {"color": "#666666"}, "* 请输入手机号码", false);
    }).on("blur", function () {
        if (phone.val() != null && phone.val() != "") {
            validateTip(phone.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(phone.next(), {"color": "red"}, imgNo + " 手机号码不能为空，请重新输入", false);
        }
    });
    // businessCode.on("focus", function () {
    //     validateTip(businessCode.next(), {"color": "#666666"}, "* 请输入业务号码", false);
    // }).on("blur", function () {
    //
    //     $.ajax({
    //         type:"GET",//请求类型
    //         url:path+"/check.json",//请求的url
    //         data:{businessCode:businessCode.val()},//请求参数
    //         // dataType:"json",//ajax接口（请求url）返回的数据类型
    //         success:function(data){//data：返回数据（json对象）
    //             if(data > 0){//存在，错误提示
    //                 validateTip(businessCode.next(),{"color":"red"},imgNo+ " 该合同已存在",false);
    //             }else{//可用，正确提示
    //                 validateTip(businessCode.next(),{"color":"green"},imgYes+" 该业务号可以使用",true);
    //             }
    //         },
    //         error:function(data){//当访问时候，404，500 等非200的错误状态码
    //             validateTip(businessCode.next(),{"color":"red"},imgNo+" 您访问的页面不存在",false);
    //         }
    //     });

        businessCode.on("focus", function () {
            validateTip(businessCode.next(), {"color": "#666666"}, "* 请输入业务号码", false);
        }).on("blur", function () {
            if (businessCode.val() != null && businessCode.val() != "") {
                validateTip(businessCode.next(), {"color": "green"}, imgYes, true);
            } else {
                validateTip(businessCode.next(), {"color": "red"}, imgNo + " 业务号码不能为空，请重新输入", false);
            }
        });

        // if (businessCode.val() != null && businessCode.val() != "") {
        //     validateTip(businessCode.next(), {"color": "green"}, imgYes, true);
        // } else {
        //     validateTip(businessCode.next(), {"color": "red"}, imgNo + " 业务号码不能为空，请重新输入", false);
        // }
    inDate.on("focus", function () {
        validateTip(inDate.next(), {"color": "#666666"}, "* 请输入入网时间", false);
    }).on("blur", function () {
        if (inDate.val() != null && inDate.val() != "") {
            validateTip(inDate.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(inDate.next(), {"color": "red"}, imgNo + " 入网时间不能为空，请重新输入", false);
        }
    });
    price.on("focus", function () {
        validateTip(price.next(), {"color": "#666666"}, "* 请输入月租金额", false);
    }).on("blur", function () {
        if (price.val() != null && price.val() != "") {
            validateTip(price.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(price.next(), {"color": "red"}, imgNo + " 月租金额不能为空，请重新输入", false);
        }
    });
    contractTerm.on("focus", function () {
        validateTip(contractTerm.next(), {"color": "#666666"}, "* 请输入合同期限", false);
    }).on("blur", function () {
        if (contractTerm.val() != null && contractTerm.val() != "") {
            validateTip(contractTerm.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(contractTerm.next(), {"color": "red"}, imgNo + " 合同期限不能为空，请重新输入", false);
        }
    });
    endDate.on("focus", function () {
        validateTip(endDate.next(), {"color": "#666666"}, "* 请输入到期日期", false);
    }).on("blur", function () {
        if (endDate.val() != null && endDate.val() != "") {
            validateTip(endDate.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(endDate.next(), {"color": "red"}, imgNo + " 到期日期不能为空，请重新输入", false);
        }
    });
    payment.on("focus", function () {
        validateTip(payment.next(), {"color": "#666666"}, "* 请输入付费方式", false);
    }).on("blur", function () {
        if (payment.val() != null && payment.val() != "") {
            validateTip(payment.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(payment.next(), {"color": "red"}, imgNo + " 付费方式不能为空，请重新输入", false);
        }
    });
    businessType.on("focus", function () {
        validateTip(businessType.next(), {"color": "#666666"}, "* 请输入业务类型", false);
    }).on("blur", function () {
        if (businessType.val() != null && businessType.val() != "") {
            validateTip(businessType.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(businessType.next(), {"color": "red"}, imgNo + " 业务类型不能为空，请重新输入", false);
        }
    });
    bandwidth.on("focus", function () {
        validateTip(bandwidth.next(), {"color": "#666666"}, "* 请输入带宽", false);
    }).on("blur", function () {
        if (bandwidth.val() != null && bandwidth.val() != "") {
            validateTip(bandwidth.next(), {"color": "green"}, imgYes, true);
        } else {
            validateTip(bandwidth.next(), {"color": "red"}, imgNo + " 带宽不能为空，请重新输入", false);
        }
    });
    state.on("focus", function () {
        validateTip(state.next(), {"color": "#666666"}, "* 请选择开通状态", false);
    }).on("blur", function () {
        // if (state.val() != null && state.val() != "") {
        //     validateTip(state.next(), {"color": "green"}, imgYes, true);
        // } else {
        //     validateTip(state.next(), {"color": "red"}, imgNo + " 供应商不能为空，请选择", false);
        // }
    });
	
	addBtn.on("click",function(){
        flowCode.blur();
        enterpriseName.blur();
        agent.blur();
        phone.blur();
        businessCode.blur();
        inDate.blur();
        price.blur();
        contractTerm.blur();
        endDate.blur();
        payment.blur();
        businessType.blur();
        bandwidth.blur();

        if (
            flowCode.attr("validateStatus") == "true" &&
            enterpriseName.attr("validateStatus") == "true" &&
            agent.attr("validateStatus") == "true" &&
            phone.attr("validateStatus") == "true" &&
            businessCode.attr("validateStatus") == "true" &&
            inDate.attr("validateStatus") == "true" &&
            price.attr("validateStatus") == "true" &&
            contractTerm.attr("validateStatus") == "true" &&
            endDate.attr("validateStatus") == "true" &&
            payment.attr("validateStatus") == "true" &&
            businessType.attr("validateStatus") == "true" &&
            bandwidth.attr("validateStatus") == "true") {
            if (confirm("是否确认提交数据")) {
                $("#billForm").submit();
            }
        }
	});
	
	backBtn.on("click",function(){
		if(referer != undefined 
			&& null != referer 
			&& "" != referer
			&& "null" != referer
			&& referer.length > 4){
		 window.location.href = referer;
		}else{
			history.back(-1);
		}
	});
});